<template>
	<view class="my-settle-container">
		<view class="quanxuan">
			<radio :checked="allState" color="#c00000" @click="radioClickHandler"></radio>
			全选
		</view>
		<view class="heji">
			合计：<text>￥{{checkedPrice}}</text>
		</view>
		<view class="jiesuan" @click="settlement">
			结算（{{checkedCount}}）
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapGetters,
		mapMutations
	} from 'vuex'
	export default {
		data() {
			return {
				seconds: 3,
				// 定时器id
				timer: null
			}
		},
		methods: {
			...mapMutations('m_cart', ['reverseSatate']),
			radioClickHandler() {
				this.reverseSatate(this.allState)
			},
			//展示结算的提示消息
			showTips(n) {
				// 调用uni.showToast()方法,展示提示消息
				uni.showToast({
					//不展示任何图标
					icon: 'none',
					//提示消息
					title: '请登录后再结算！' + n + '秒后自动跳转到登录页面',
					//为页面添加透明遮罩，防止点击
					mask: true,
					//1.5秒后消失
					duration: 1500
				})
			},
			settlement() {
				//1判断是否勾选了要结算的商品
				if (!this.checkedCount) return uni.$showMsg("请选择要结算的商品！")
				if (!this.address.cityName) return uni.$showMsg("请选择收货地址！")
				// if (!this.token) return uni.$showMsg("请先登录！")
				if (!this.token) return this.delayNavigate()
				// console.log(this.address)
			},
			// n秒后导航到my页面
			delayNavigate() {
				//1展示提示消息，此时seconds值等于3
				this.showTips(this.seconds)
				//2创建定时器，每隔1秒执行一次
				this.timer = setInterval(() => {
					this.seconds--
					if (this.seconds <= 0) {
						clearInterval(this.timer)
						uni.switchTab({
							url: '/pages/my/my'
						})
					}
					this.showTips(this.seconds)
				}, 1000)
			}
		},
		computed: {
			...mapGetters('m_cart', ['checkedCount', 'checkedPrice', 'allState']),
			...mapState('m_user', ['address', 'token'])
		}
	}
</script>
<style lang="scss">
	.my-settle-container {
		position: fixed;
		bottom: 0;
		left: 0;
		z-index: 999;
		height: 50px;
		width: 100%;
		display: flex;
		justify-content: space-between;
		align-items: center;
		background-color: #fff;

		.quanxuan {
			margin-left: 10px;
			font-size: 14px;
			height: 50px;
			// background-color: red;
			display: flex;
			align-items: center;
		}

		.heji {
			font-size: 14px;

			text {
				color: #aa0001;
			}
		}

		.jiesuan {
			background-color: #aa0001;
			width: 125px;
			height: 50px;
			display: flex;
			justify-content: center;
			align-items: center;
			font-size: 14px;
			color: #fff;
		}
	}
</style>
